/*
  Martin Nawrath KHM LAB3
  Kunsthochschule f¸r Medien Kˆln
  Academy of Media Arts
  http://www.khm.de
  http://interface.khm.de/index.php/labor/experimente/
 */
#include <Arduino.h>
#include <FreqCounter.h>
#include <LiquidCrystal.h>

unsigned long frq;
float frekuensi;
int cnt;
int pinLed =13;

// Inisialisasi koneksi LCD
LiquidCrystal lcd(12, 11, 10, 9, 8, 7, 6);


void nyalakan_led()
{
 delay(20);
 digitalWrite(pinLed,!digitalRead(pinLed));  // blink Led
}

void setup()
{
  pinMode(pinLed, OUTPUT);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Freq Counter");
  lcd.setCursor(0,1); lcd.print("Oleh MD4N1 2013");
  delay(500);
  lcd.clear();
}

void loop()
{

  // wait if any serial is going on
  FreqCounter::f_comp=10;   // Cal Value / Calibrate with professional Freq Counter
  FreqCounter::start(100);  // 100 ms Gate Time

  while (FreqCounter::f_ready == 0)

  frq=FreqCounter::f_freq;
  frq = frq*10;
  if (frq < 1000)
  {
      nyalakan_led();
      lcd.clear();
      lcd.setCursor(0,0);lcd.clear();lcd.print("Frek: "); lcd.print(frq); lcd.print("Hz");

  }
  else if (frq < 1000000)
  {
    frekuensi = frq*0.001;
    nyalakan_led();
    lcd.setCursor(0,0);lcd.clear();lcd.print("Frek: "); lcd.print(frekuensi); lcd.print("KHz");

  }

  else
  {
    frekuensi = frq*0.000001;
    nyalakan_led();
    lcd.setCursor(0,0);lcd.clear();lcd.print("Frek: "); lcd.print(frekuensi); lcd.print("MHz");
  }

}

